*** Stata 15 reproduction code for

*Mader, Matthias & Harald Schoen.
*What a difference an event can make: The European refugee crisis, party competition, and voters’ responses in Germany
*West European Politics

*** Figure 1: Immigration salience, perceived party positions, and attitudes, 2013-2017
	
*** Mentions of important problems

	* CREATE DATASET FOR LINEPLOT

	use "DATA_Figure1.dta", clear // created in "000_Prepare_tracking_data"

	gen month = .			
	replace month = 	1	if track ==	19
	replace month = 	5	if track ==	20
	replace month = 	9	if track ==	21
	replace month = 	11	if track ==	22
	replace month = 	14	if track ==	23
	replace month = 	17	if track ==	24
	replace month = 	20	if track ==	25
	replace month = 	23	if track ==	26
	replace month = 	26	if track ==	27
	replace month = 	30	if track ==	28
	replace month = 	33	if track ==	29
	replace month = 	36	if track ==	30
	replace month = 	38	if track ==	31
	replace month = 	42	if track ==	32
	replace month = 	44	if track ==	33
	replace month = 	48	if track ==	34
	replace month = 	51	if track ==	35
	replace month = 	54	if track ==	36
	replace month = 	57	if track ==	37

	tempname line
	postfile `line' month mip_immi_M mip_immi_L mip_immi_U mip_labor_M mip_labor_L mip_labor_U using Graph.dta, replace

	forvalues y = 1/60 {
	foreach var in mip_immi mip_labor {
	ci means `var' [aw = weight1] if month==`y'
	scalar `var'_M = r(mean)
	scalar `var'_L = r(lb)
	scalar `var'_U = r(ub)
	}
	scalar monat = `y'

	post `line' (monat) (mip_immi_M) (mip_immi_L) (mip_immi_U) (mip_labor_M) (mip_labor_L) (mip_labor_U)
	}
	postclose `line'

	* LINEPLOT

	use Graph.dta, clear

	#delimit ;
	twoway  rspike mip_immi_L mip_immi_U month, lwidth(thick) color(blue) ||
		connected mip_immi_M month, sort clpattern(dash) lwidth(thick) clcolor(blue) mcolor(blue)  msymbol(i) ||
		rspike mip_labor_L mip_labor_U month, lwidth(thick) color(red) ||
		connected mip_labor_M month, sort clpattern(dash) lwidth(thick) clcolor(red) mcolor(red) msymbol(i)
		legend(off)
		name(mip_tracking, replace)
 		text(.45 51 "Immigration/Refugees", color(blue)  size(large)) text(.15 53 "Labour market", color(red) size(large))
		xlabel(1 "1/13" 13 "1/14" 25 "1/15" 37 "1/16" 49 "1/17" 61 "1/18", angle(0) labsize(large))
		ylabel(0(.2)1, angle(0) nogrid labsize(large))
		xtitle("")
		ytitle("Fraction", angle(90)  size(large))
		graphregion(color(white))
		title("Mentions of important problems", color(gs0) size(large))
		;
	#delimit cr


*** Support for restricting immigration

	* CREATE DATASET FOR LINEPLOT
	
	*MIP (Trackings)
	use "DATA_Figure1.dta", clear

	gen month = .			
	replace month = 	5	if track ==	20
	replace month = 	9	if track ==	21
	replace month = 	17	if track ==	24
	replace month = 	30	if track ==	28
	replace month = 	36	if track ==	30
	replace month = 	38	if track ==	31
	replace month = 	42	if track ==	32
	replace month = 	44	if track ==	33
	replace month = 	48	if track ==	34
	replace month = 	51	if track ==	35
	replace month = 	54	if track ==	36
	replace month = 	57	if track ==	37

	tempname line
	postfile `line' month track_immi_M track_immi_L track_immi_U using Graph1a.dta, replace

	forvalues y = 1/60 {
	foreach var in immi {
	ci means `var' [aw = weight1] if month==`y'
	scalar `var'_M = r(mean)
	scalar `var'_L = r(lb)
	scalar `var'_U = r(ub)
	}
	scalar monat = `y'

	post `line' (monat) (immi_M) (immi_L) (immi_U)
	}
	postclose `line'

	*MIP (Panel)
	use "DATA_long", clear // created in "0000_Prepare_panel_data"
	gen month = .
	replace month = 	6	if wave ==	1
	replace month = 	7	if wave ==	2
	replace month = 	8	if wave ==	3
	replace month = 	8	if wave ==	4
	replace month = 	9	if wave ==	5
	replace month = 	9	if wave ==	6	
	replace month = 	9	if wave ==	7
	replace month = 	33	if wave ==	9	
	replace month = 	46	if wave ==	10
	replace month = 	50	if wave ==	11
	replace month = 	53	if wave ==	12
	replace month = 	55	if wave ==	13
	replace month = 	56	if wave ==	14
	replace month = 	57	if wave ==	15
	replace month = 	58	if wave ==	16
	replace month = 	58	if wave ==	17
	
	tempname line
	postfile `line' month panel_immi_M panel_immi_L panel_immi_U using Graph1b.dta, replace

	forvalues y = 1/60 {
	foreach var in immi {
	ci means `var' if month==`y' & filter_immi1 == 1
	scalar `var'_M = r(mean)
	scalar `var'_L = r(lb)
	scalar `var'_U = r(ub)
	}
	scalar monat = `y'

	post `line' (monat) (immi_M) (immi_L) (immi_U)
	}
	postclose `line'
	
	* LINEPLOT

	use Graph1a.dta, clear
	merge 1:1 month using Graph1b.dta

	#delimit ;
	twoway  rspike track_immi_L track_immi_U month, lwidth(thick) color(gs0) ||
		connected track_immi_M month, sort clpattern(dash) lwidth(thick) clcolor(gs0) mcolor(gs0)  msymbol(i) ||
			rspike panel_immi_L panel_immi_U month, lwidth(thick) color(gs11) ||
		connected panel_immi_M month, sort clpattern(dash) lwidth(thick) clcolor(gs11) mcolor(gs11) msymbol(i)
		legend(off)
		name(immi, replace)
		text(.52 27 "Tracking", color(gs0)  size(large)) text(.75 55 "Panel", color(gs11) size(large))
		xlabel(1 "1/13" 13 "1/14" 25 "1/15" 37 "1/16" 49 "1/17" 61 "1/18", angle(0) labsize(large))
		ylabel(0(.2)1, angle(0) nogrid labsize(large))
		xtitle("")
		ytitle("Mean score", angle(90) size(large))
		graphregion(color(white))
		title("Support for restricting immigration", color(gs0) size(large))
		;
	#delimit cr


*** Perceived party position on immigration

	* CREATE DATASET FOR LINEPLOT

	use "DATA_Figure1.dta", clear
	
	gen month = .			
	replace month = 	1	if track ==	19
	replace month = 	5	if track ==	20
	replace month = 	9	if track ==	21
	replace month = 	11	if track ==	22
	replace month = 	14	if track ==	23
	replace month = 	17	if track ==	24
	replace month = 	20	if track ==	25
	replace month = 	23	if track ==	26
	replace month = 	26	if track ==	27
	replace month = 	30	if track ==	28
	replace month = 	33	if track ==	29
	replace month = 	36	if track ==	30
	replace month = 	38	if track ==	31
	replace month = 	42	if track ==	32
	replace month = 	44	if track ==	33
	replace month = 	48	if track ==	34
	replace month = 	51	if track ==	35
	replace month = 	54	if track ==	36
	replace month = 	57	if track ==	37

	tempname line
	postfile `line' month immi_cdu_M immi_cdu_L immi_cdu_U immi_csu_M immi_csu_L immi_csu_U immi_afd_M immi_afd_L immi_afd_U immi_spd_M immi_spd_L immi_spd_U ///
						  immi_gruene_M immi_gruene_L immi_gruene_U immi_linke_M immi_linke_L immi_linke_U using Graph1.dta, replace
	
	forvalues y = 1/60 {
	foreach var in immi_cdur immi_csur immi_afdr immi_spdr immi_gruener immi_linker {
	ci means `var' [aw = weight1] if month==`y'
	scalar `var'_M = r(mean)
	scalar `var'_L = r(lb)
	scalar `var'_U = r(ub)
	}
	scalar monat = `y'

	post `line' (monat) (immi_cdur_M) (immi_cdur_L) (immi_cdur_U) (immi_csur_M) (immi_csur_L) (immi_csur_U) (immi_afdr_M) (immi_afdr_L) ///
						(immi_afdr_U) (immi_spdr_M) (immi_spdr_L) (immi_spdr_U) ///
						(immi_gruener_M) (immi_gruener_L) (immi_gruener_U)	(immi_linker_M) (immi_linker_L) (immi_linker_U)	
	}
	postclose `line'

	*  LINEPLOT

	use Graph1.dta, clear

	#delimit ;
	twoway rspike immi_cdu_L immi_cdu_U month, color(black) lwidth(thick) ||
		connected immi_cdu_M month, sort clpattern(dash) lwidth(thick) clcolor(black) mcolor(black)  msymbol(i) ||
		rspike immi_csu_L immi_csu_U month, color(ltblue) lwidth(thick) ||
		connected immi_csu_M month, sort clpattern(dash) lwidth(thick) clcolor(ltblue) mcolor(ltblue) msymbol(i)  ||
		rspike immi_afd_L immi_afd_U month, color(blue) lwidth(thick) ||
		connected immi_afd_M month, sort clpattern(dash) lwidth(thick) clcolor(blue) mcolor(blue)  msymbol(i) ||
		rspike immi_spd_L immi_spd_U month, color(red) lwidth(thick) ||
		connected immi_spd_M month, sort clpattern(dash) lwidth(thick) clcolor(red) mcolor(red)  msymbol(i) ||
		rspike immi_gruene_L immi_gruene_U month, color(green) lwidth(thick) ||
		connected immi_gruene_M month, sort clpattern(dash) lwidth(thick) clcolor(green) mcolor(green)  msymbol(i) ||
		rspike immi_linke_L immi_linke_U month, color(cranberry) lwidth(thick) ||
		connected immi_linke_M month, sort clpattern(dash) lwidth(thick) clcolor(cranberry) mcolor(cranberry)  msymbol(i)
		legend(off)
		name(perveived_pp, replace)
		text(.96 61 "AfD", color(blue) size(large))
		text(.71 61 "CSU", color(ltblue) size(large))
		text(.5 61 "CDU", color(black) size(large))
		text(.4 61 "SPD", color(red)  size(large))
		text(.34 61 "Left", color(cranberry) size(large))
		text(.29 62 "Greens", color(green) size(large))		
		xlabel(1 "1/13" 13 "1/14" 25 "1/15" 37 "1/16" 49 "1/17" 61 "1/18", angle(0) labsize(large))
		ylabel(0(.2)1, angle(0) nogrid labsize(large))
		xtitle("")
		ytitle("Mean score", angle(90) size(large))
		graphregion(color(white) lwidth(large))
		title("Perceived party position on immigration", color(gs0) size(large))
		;
	#delimit cr	


*** Support for restricting immigration among partisans

	use "DATA_Figure1.dta", clear

	gen month = .			
	replace month = 	1	if track ==	19
	replace month = 	5	if track ==	20
	replace month = 	9	if track ==	21
	replace month = 	11	if track ==	22
	replace month = 	14	if track ==	23
	replace month = 	17	if track ==	24
	replace month = 	20	if track ==	25
	replace month = 	23	if track ==	26
	replace month = 	26	if track ==	27
	replace month = 	30	if track ==	28
	replace month = 	33	if track ==	29
	replace month = 	36	if track ==	30
	replace month = 	38	if track ==	31
	replace month = 	42	if track ==	32
	replace month = 	44	if track ==	33
	replace month = 	48	if track ==	34
	replace month = 	51	if track ==	35
	replace month = 	54	if track ==	36
	replace month = 	57	if track ==	37
	
	tempname line
	postfile `line' monat ///
	immi_2_M immi_2_L immi_2_U /// 2 = cdu
	immi_3_M immi_3_L immi_3_U /// 3 = csu
	immi_4_M immi_4_L immi_4_U /// 4 = spd
	immi_5_M immi_5_L immi_5_U /// 5 = fdp
	immi_6_M immi_6_L immi_6_U /// 6 = greens
	immi_7_M immi_7_L immi_7_U /// 7 = left
	immi_322_M immi_322_L immi_322_U /// 322 = afd
	immi_808_M immi_808_L immi_808_U /// 808 = none
	using Graph.dta, replace

	forvalues y = 1/60 {
	foreach x in 2 3 4 5 6 7 322 808 {
	ci means immi [aw = weight1] if pid == `x' & month==`y'
	scalar immi_`x'_M = r(mean)
	scalar immi_`x'_L = r(lb)
	scalar immi_`x'_U = r(ub)
	}
	scalar monat = `y'

	post `line' (monat) (immi_2_M) (immi_2_L) (immi_2_U) (immi_3_M) (immi_3_L) (immi_3_U) (immi_4_M) (immi_4_L) (immi_4_U) ///
	(immi_5_M) (immi_5_L) (immi_5_U) (immi_6_M) (immi_6_L) (immi_6_U) (immi_7_M) (immi_7_L) (immi_7_U)  ///
	(immi_322_M) (immi_322_L) (immi_322_U) (immi_808_M) (immi_808_L) (immi_808_U)
	}
	postclose `line'


*** LINEPLOT

	use Graph.dta, clear

	#delimit ;
	twoway rspike immi_2_L immi_2_U monat, lwidth(thick) color(black) ||
		connected immi_2_M monat, sort clpattern(dash) lwidth(thick) clcolor(black) mcolor(black)  msymbol(i) ||
		///
		rspike immi_3_L immi_3_U monat, lwidth(thick) color(ltblue) ||
		connected immi_3_M monat, sort clpattern(dash) lwidth(thick) clcolor(ltblue) mcolor(ltblue) msymbol(i)  ||
		///
		rspike immi_4_L immi_4_U monat, lwidth(thick) color(red) ||
		connected immi_4_M monat, sort clpattern(dash) lwidth(thick) clcolor(red) mcolor(red)  msymbol(i) ||
		///
		rspike immi_6_L immi_6_U monat, lwidth(thick) color(midgreen) ||
		connected immi_6_M monat, sort clpattern(dash) lwidth(thick) clcolor(midgreen) mcolor(midgreen)  msymbol(i) ||
		///
		rspike immi_322_L immi_322_U monat, lwidth(thick) color(blue) ||
		connected immi_322_M monat, sort clpattern(dash) lwidth(thick) clcolor(blue) mcolor(blue)  msymbol(i)
		///
		text(.61 61 "CDU", size(large) color(black))
		text(.80 61 "CSU", size(large) color(ltblue))
		text(.51 61 "SPD", size(large) color(red))
		text(.37 61 "Greens", size(medlarge) color(midgreen))
		text(.94 61 "AfD", size(large) color(blue))
		///
		legend(off)
		name(immi_partisans, replace)
 		xlabel(1 "1/13" 13 "1/14" 25 "1/15" 37 "1/16" 49 "1/17" 61 "1/18", angle(0) labsize(large))
		ylabel(0(.2)1, angle(0) nogrid labsize(large))
		xtitle("")
		ytitle("Mean score", angle(90)  size(large))
		graphregion(color(white))
		title("Support for restricting immigration among partisans", color(gs0) size(large))
		;
		#delimit cr
	
	rm Graph.dta
